<html> 
  <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Tryn-Trava</title> 
  </head>
<body>

<pre>
START
<?php

function sosed($vopr, $herd, $num)
{
 if($vopr==$herd)
 {
   return(100);
 }

 $rez = $herd - 1;
 if ($rez==0)
 {
   $rez = $num;
 }
 if($vopr==$rez)
 {
   return(1);
 }

 $rez = $herd + 1;
 if ($rez==($num + 1))
 {
   $rez = 1;
 }
 if($vopr==$rez)
 {
   return(1);
 }


 $rez = $herd - 2;
 if ($rez==0)
 {
   $rez = $num;
 }
 if ($rez==-1)
 {
   $rez = $num - 1;
 }
 if($vopr==$rez)
 {
   return(2);
 }

 $rez = $herd + 2;
 if ($rez==($num + 1))
 {
   $rez = 1;
 }
 if ($rez==($num + 2))
 {
   $rez = 2;
 }
 if($vopr==$rez)
 {
   return(2);
 }

 return(0);
}

function ver_res($type)
{
      global $j;
      global $pack;
      $sql_packs = "select tt_cards.id, 
                           tt_cards.name, 
                           tt_rares.rare,
                           tt_rares.index 
                    from tt_cards, tt_rares, tt_classes 
                    where tt_rares.id = tt_cards.rare and
                          tt_classes.id = tt_cards.class and 
                          tt_rares.rare in ('common','uncommon','rare') and
                          tt_cards.type = ".$type." and
                          tt_classes.class in ('change','change_all','killprot','special2','special',want-type)";
      $tt_packs = mysql_query($sql_packs);
      while($tt_pack = mysql_fetch_array($tt_packs))
      {
        for($i=1;$i<=3;$i++)
        {
          $pack[$j++]=array("id" => $tt_pack["id"],"name" => $tt_pack["name"],"rare" => $tt_pack["rare"]);
        }    
      }
 return("OK");
}

function gmod($type)
{
      global $goroskop;
      if ($type==$goroskop)
      {
        return(1.5);
      }
      else
      {
        return(1);
      }

}

function kill_cards($type, $sts, $on_herd, $game, $year)
{
  $year1 = $year + 1;
  $tt_result = mysql_query(
                    "update tt_games_cards,tt_cards
                     set tt_games_cards.sts = 9, 
                         tt_games_cards.kill_year = ".$year1."
                     where tt_games_cards.sts = ".$sts." and 
                           tt_games_cards.on_herd  = ".$on_herd." and 
                           tt_games_cards.game = ".$game." and
                           tt_cards.id = tt_games_cards.card and
                           tt_cards.type = ".$type);
 print mysql_error();
 if($sts==1)
 {
   $tt_result = mysql_query(
                    "update tt_games_cards,tt_cards
                     set tt_games_cards.sts = 9, 
                         tt_games_cards.kill_year = ".$year1."
                     where tt_games_cards.sts = 0 and 
                           tt_games_cards.on_herd  = ".$on_herd." and 
                           tt_games_cards.game = ".$game." and
                           tt_cards.id = tt_games_cards.card and
                           tt_cards.class <> 2 and
                           tt_cards.type = ".$type);
   print mysql_error();
 }
 return("OK");
}

#ini_set('error_reporting', E_ALL);
#ini_set('display_errors','On'); 

#mysql_connect('localhost', 'tt', 'tt123');
#mysql_select_db('tt');

#$tt_result = mysql_query('SET NAMES "utf8"');
#print mysql_error(); 
#$tt_result = mysql_query('START TRANSACTION;');
#print mysql_error();
 
session_start();
include("game/connect.php");

$tt_result = mysql_query("START TRANSACTION;");
print mysql_error(); 

$sql_games = "select * from tt_games where tt_games.sts = 1";
$tt_games = mysql_query($sql_games);
print mysql_error(); 
while($tt_game = mysql_fetch_array($tt_games))
{
  $goroskop = ((($tt_game["year"] + 1) % 12) + 1);
  $next_year = $tt_game["year"] + 1;
  # snosim karti, kotorije dolzhni idti pod snos
  # tt_games_cards.sts = 0 - na rukah, 1 - v igre, 9 - vishla
  # tt_games_cards.on_herd = -1 - pod snos
  $tt_result = mysql_query("update tt_games_cards 
                            set tt_games_cards.sts = 9,
                                tt_games_cards.on_year = ".$tt_game["year"]."
                            where tt_games_cards.sts = 0 and 
                                  tt_games_cards.on_herd = -1");
  print mysql_error(); 

  # obrabativajem stada zhivotnih  
  $sql_herds = "select * from tt_games_herds where tt_games_herds.sts = 1 and tt_games_herds.game = ".$tt_game["id"];
  $tt_herds = mysql_query($sql_herds);
  while($tt_herd = mysql_fetch_array($tt_herds))
  {
    print "  HERD:".$tt_herd["id"]."\n";
    $req_cards_count=7;

    if($tt_game["year"]>0)
    { # obschjot igroka
      # ustanovka nachalnih znachenij
      $g["Z"] =$tt_herd["Z"];  #kollichestvo zaitcev
      $g["PZ"]=$tt_herd["PZ"]; #prirost zatcev
      $g["V"] =$tt_herd["V"];  #kollichestvo volkov
      $g["PV"]=$tt_herd["PV"]; #smertnost volkov
      $g["TT"]=$tt_herd["TT"]; #kollichestvo tryn-travy
      $g["PT"]=$tt_herd["PT"]; #porcha travi
      $g["B"] =$tt_herd["B"];  #volchij ulov zaitcev
      $g["E"] =$tt_herd["E"];  #porcija zajtcev na rozhdenije odnogo volka
      $g["TZ"]=$tt_herd["TZ"]; #sbor travi odnim zajtcem

      print "    YEAR:".$tt_game["year"]."\n";

      #stihijnije karti
      $rand_m = mt_rand(1,12);
      if($tt_game["year"]==1)
      {
        $rand_play = 1;
      }
      else
      {
        $rand_play = mt_rand(1,5);
        print "    RANDOM ".$rand_play."\n";
        # $rand_play = 1;
      }
      if($rand_play==1)
      {
        $rand_a="";
        $j=0;
        $sql_packs = "select tt_cards.id 
                      from tt_cards, tt_rares 
                      where tt_rares.id = tt_cards.rare and 
                            tt_rares.rare = 'weather'";
        $tt_packs = mysql_query($sql_packs);
        while($tt_pack = mysql_fetch_array($tt_packs))
        {
          $rand_a[$j++] = $tt_pack["id"];
        }
        $rand_num = $rand_a[array_rand($rand_a,1)];
        $tt_result = mysql_query("insert into tt_games_cards
                                (game,herd,card,year,on_herd,sts)
                                values(".$tt_game["id"].",0,".$rand_num.",".$tt_game["year"].",".$tt_herd["id"].",1)");
        print mysql_error();
      }

      # rassejannostj
      $rassej = 0;
      $sql_gcards = "select tt_games_cards.*
                       from tt_games_cards 
                       where tt_games_cards.sts = 0 and 
                             tt_games_cards.card = 277 and
                             tt_games_cards.on_herd  = ".$tt_herd["id"]." and
                             tt_games_cards.game = ".$tt_game["id"];
      $tt_gcards = mysql_query($sql_gcards);
      print mysql_error();
      while($tt_gcard = mysql_fetch_array($tt_gcards))
      {
        print "        RASSEJANOSTJ ON ".$tt_gcard["on_herd"]."\n";
        $rassej = 1;
      }
      if ($rassej==1)
      {
        $sql_gcards = "select tt_games_cards.id
                       from tt_games_cards 
                       where tt_games_cards.sts = 0 and 
                             tt_games_cards.on_herd = 0 and
                             tt_games_cards.herd  = ".$tt_herd["id"]." and
                             tt_games_cards.game = ".$tt_game["id"];
        $tt_gcards = mysql_query($sql_gcards);
        print mysql_error();
        $rass_get =  0;
        while($tt_gcard = mysql_fetch_array($tt_gcards))
        {
          if ($rass_get==0)
          {
            $tt_result = mysql_query("update tt_games_cards 
                            set tt_games_cards.on_herd = ".$tt_herd["id"]."
                            where tt_games_cards.id = ".$tt_gcard["id"]);
            print mysql_error();
          }
          $rass_get = 1;
        }
      }

      # karti v igre unichtozhajut karti na rukah.
      # function kill_cards($type, $sts, $tt_herd["id"], $tt_game["id"], $tt_game["year"])
      
      $sql_gcards = "select tt_games_cards.*,tt_cards_actions.action
                       from tt_games_cards, tt_cards, tt_cards_actions 
                       where tt_games_cards.sts = 1 and 
                             tt_games_cards.on_herd = ".$tt_herd["id"]." and
                             tt_games_cards.game = ".$tt_game["id"]." and
                             tt_cards.id = tt_games_cards.card and
                             tt_cards.class = 2 and
                             tt_cards_actions.card = tt_cards.id
                       ";      
      $tt_gcards = mysql_query($sql_gcards);
      print mysql_error();
      while($tt_gcard = mysql_fetch_array($tt_gcards))
      {
        if($tt_gcard['action']==19)
        {
          $kill_res = kill_cards(1, 0, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==20)
        {
          $kill_res = kill_cards(2, 0, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==36)
        {
          $kill_res = kill_cards(3, 0, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==16)
        {
          $kill_res = kill_cards(4, 0, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==25)
        {
          $kill_res = kill_cards(5, 0, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==21)
        {
          $kill_res = kill_cards(6, 0, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==11)
        {
          $kill_res = kill_cards(7, 0, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==8)
        {
          $kill_res = kill_cards(8, 0, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==12)
        {
          $kill_res = kill_cards(9, 0, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==22)
        {
          $kill_res = kill_cards(10, 0, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==9)
        {
          $kill_res = kill_cards(11, 0, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==10)
        {
          $kill_res = kill_cards(12, 0, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
      }

      # karti v na rukah unichtozhajut karti v igre i na rukah.
      # function kill_cards($type, $sts, $tt_herd["id"], $tt_game["id"], $tt_game["year"])
      
      $sql_gcards = "select tt_games_cards.*,tt_cards_actions.action
                       from tt_games_cards, tt_cards, tt_cards_actions 
                       where tt_games_cards.sts = 0 and 
                             tt_games_cards.on_herd = ".$tt_herd["id"]." and
                             tt_games_cards.game = ".$tt_game["id"]." and
                             tt_cards.id = tt_games_cards.card and
                             tt_cards.class = 2 and
                             tt_cards_actions.card = tt_cards.id
                       ";      
      $tt_gcards = mysql_query($sql_gcards);
      print mysql_error();
      while($tt_gcard = mysql_fetch_array($tt_gcards))
      {
        if($tt_gcard['action']==19)
        {
          $kill_res = kill_cards(1, 1, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==20)
        {
          $kill_res = kill_cards(2, 1, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==36)
        {
          $kill_res = kill_cards(3, 1, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==16)
        {
          $kill_res = kill_cards(4, 1, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==25)
        {
          $kill_res = kill_cards(5, 1, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==21)
        {
          $kill_res = kill_cards(6, 1, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==11)
        {
          $kill_res = kill_cards(7, 1, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==8)
        {
          $kill_res = kill_cards(8, 1, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==12)
        {
          $kill_res = kill_cards(9, 1, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==22)
        {
          $kill_res = kill_cards(10, 1, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==9)
        {
          $kill_res = kill_cards(11, 1, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==10)
        {
          $kill_res = kill_cards(12, 1, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
        if($tt_gcard['action']==24)
        {
          for($ti=1;$ti<=12;$ti++)
          {
            $kill_res = kill_cards($ti, 1, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
          }
        }
        if($tt_gcard['action']==33)
        {
          $kill_res = kill_cards(((($tt_game["year"] + 1) % 12) + 1), 1, $tt_herd["id"], $tt_game["id"], $tt_game["year"]);
        }
      }
      
      $listok = 0;
      $pero = 0;
      $v_zajac = 0;
      $v_volk = 0;
      #var_dump($g); 
      # bezhim po mesjacam
      for($m=1;$m<=12;$m++)
      {
        print "      MONTH:".$m."\n";
        # sostavljajem spisok kart, kotorije igrajut na dannogo igroka
        # i nahodim delti vseh parametrov dlja dannogo mesjaca
        $gd["Z"] =0;
        $gd["PZ"]=0;
        $gd["V"] =0;
        $gd["PV"]=0;
        $gd["TT"]=0;
        $gd["PT"]=0;
        $gd["B"] =0;
        $gd["E"] =0;
        $gd["TZ"]=0;

        print "        Z =".$g["Z"].", ";
        print         "PZ=".$g["PZ"].", ";
        print         "V =".$g["V"].", "; 
        print         "PV=".$g["PV"].", ";
        print         "TT=".$g["TT"].", ";
        print         "PT=".$g["PT"].", ";
        print         "B =".$g["B"].", "; 
        print         "E =".$g["E"].", "; 
        print         "TZ=".$g["TZ"]."\n";
        
        # zajac-shpion
        if($m==1)
        {
          $sql_gcards = "select tt_games_cards.*
                       from tt_games_cards 
                       where tt_games_cards.sts = 0 and 
                             tt_games_cards.on_herd > 0 and
                             tt_games_cards.card = 151 and
                             tt_games_cards.herd  = ".$tt_herd["id"]." and
                             tt_games_cards.game = ".$tt_game["id"];
          $tt_gcards = mysql_query($sql_gcards);
          print mysql_error();
          while($tt_gcard = mysql_fetch_array($tt_gcards))
          {
            print "        SHPION ON ".$tt_gcard["on_herd"]."\n";
            $tt_result = mysql_query("update tt_games_herds 
                                      set spy_year = ".$tt_game["year"]." + 1,
                                          spy_herd = ".$tt_gcard["on_herd"]."
                         where tt_games_herds.id = ".$tt_herd["id"]);
            print mysql_error();
          }
        }

        # zholudj duba-kolduna
        if($m==1)
        {
          $sql_gcards = "select tt_games_cards.*
                       from tt_games_cards 
                       where tt_games_cards.sts = 0 and 
                             tt_games_cards.herd > 0 and
                             tt_games_cards.card = 121 and
                             tt_games_cards.on_herd  = ".$tt_herd["id"]." and
                             tt_games_cards.game = ".$tt_game["id"];
          $tt_gcards = mysql_query($sql_gcards);
          print mysql_error();
          while($tt_gcard = mysql_fetch_array($tt_gcards))
          {
            print "        ZHOLUDJ ON ".$tt_gcard["on_herd"]."\n";
            $tt_result = mysql_query("update tt_games_cards 
                                  set tt_games_cards.sts = 9 
                                  where tt_games_cards.sts = 0 and
                                        tt_games_cards.on_herd = 0 and
                                        tt_games_cards.game = ".$tt_game["id"]." and
                                        tt_games_cards.herd = ".$tt_herd["id"]);
            print mysql_error();
          }
        }

        # volshebnij listok
        if($m==1)
        {
          $sql_gcards = "select tt_games_cards.*
                       from tt_games_cards 
                       where tt_games_cards.sts = 0 and 
                             tt_games_cards.herd > 0 and
                             tt_games_cards.card = 180 and
                             tt_games_cards.on_herd  = ".$tt_herd["id"]." and
                             tt_games_cards.game = ".$tt_game["id"];
          $tt_gcards = mysql_query($sql_gcards);
          print mysql_error();
          while($tt_gcard = mysql_fetch_array($tt_gcards))
          {
            print "        VOLSHEBNIJ LISTOK ON ".$tt_gcard["on_herd"]."\n";
            $listok = 1;
          }
        }

        # volshebnoje pero
        if($m==1)
        {
          $sql_gcards = "select tt_games_cards.*
                       from tt_games_cards 
                       where tt_games_cards.sts = 0 and 
                             tt_games_cards.herd > 0 and
                             tt_games_cards.card = 184 and
                             tt_games_cards.on_herd  = ".$tt_herd["id"]." and
                             tt_games_cards.game = ".$tt_game["id"];
          $tt_gcards = mysql_query($sql_gcards);
          print mysql_error();
          while($tt_gcard = mysql_fetch_array($tt_gcards))
          {
            print "        VOLSHEBNOJE PERO ON ".$tt_gcard["on_herd"]."\n";
            $pero = 1;
          }
        }

        #vechnij volk
        if($m==1)
        {
          $sql_gcards = "select tt_games_cards.*
                       from tt_games_cards 
                       where tt_games_cards.sts in (0,1) and 
                             tt_games_cards.card = 55 and
                             tt_games_cards.on_herd  = ".$tt_herd["id"]." and
                             tt_games_cards.game = ".$tt_game["id"];
          $tt_gcards = mysql_query($sql_gcards);
          print mysql_error();
          while($tt_gcard = mysql_fetch_array($tt_gcards))
          {
            print "        VOLK ON ".$tt_gcard["on_herd"]."\n";
            $v_volk = 1;
          }
        }
        #vechnij zajac
        if($m==1)
        {
          $sql_gcards = "select tt_games_cards.*
                       from tt_games_cards 
                       where tt_games_cards.sts in (0,1) and 
                             tt_games_cards.card = 56 and
                             tt_games_cards.on_herd  = ".$tt_herd["id"]." and
                             tt_games_cards.game = ".$tt_game["id"];
          $tt_gcards = mysql_query($sql_gcards);
          print mysql_error();
          while($tt_gcard = mysql_fetch_array($tt_gcards))
          {
            print "        ZAJAC ON ".$tt_gcard["on_herd"]."\n";
            $v_zajac = 1;
          }
        }

        # smotrim karti, kotorije napjamuju na sebja igrajutsa
        $sql_gcards = "select tt_games_cards.*,tt_cards.name,tt_cards.class, tt_cards.type, tt_actions.action, tt_cards_actions.param, tt_times.time
                       from tt_games_cards,tt_cards,tt_cards_actions,tt_actions,tt_times 
                       where tt_games_cards.sts in (0,1) and 
                             (
                              (
                               tt_games_cards.on_herd  = ".$tt_herd["id"]."
                              )
                              or
                              (
                               # globalnije karti 
                               tt_games_cards.on_herd  > 0 and
                               tt_cards.class = 3
                              )
                             ) 
                             and 
                             tt_games_cards.game = ".$tt_game["id"]." and
                             tt_cards.id = tt_games_cards.card and
                             tt_cards_actions.card = tt_cards.id and
                             tt_actions.id = tt_cards_actions.action and
                             tt_times.id = tt_cards.time and
                             tt_times.time in ('".$m."','permanent','year','random')";
        $tt_gcards = mysql_query($sql_gcards);
        print mysql_error();
        while($tt_gcard = mysql_fetch_array($tt_gcards))
        {
         if(
            ($tt_gcard["time"]<>"random") or 
            (($tt_gcard["time"]=="random") and ($m==$rand_m))
           )
         {
          print "        CARD/TIME/ACTION:".$tt_gcard["name"]."/".$tt_gcard["time"]."/".$tt_gcard["action"]."=".$tt_gcard["param"] * gmod($tt_gcard["type"])."\n";
          $id = $tt_gcard["action"];
          if($id=='V_'||$id=='Z_'||$id=='TT_')
          { # obrabativajem variant uhoda parametrov k sosedu
            $id = substr($id,0,strlen($id)-1); 
          }
          $gd[$id] = $gd[$id] + $tt_gcard["param"] * gmod($tt_gcard["type"]);           
         }
        }
        # smotrim karti, kotorije kosvenno na sebja igrajutsa
        $sql_gcards = "select tt_games_cards.*,tt_cards.name, tt_cards.type, tt_actions.action, tt_cards_actions.param, tt_times.time
                       from tt_games_cards,tt_cards,tt_cards_actions,tt_actions,tt_times 
                       where tt_games_cards.sts in (0,1) and 
                             tt_games_cards.herd = ".$tt_herd["id"]." and 
                             tt_games_cards.on_herd > 0 and 
                             tt_games_cards.game = ".$tt_game["id"]." and
                             tt_cards.id = tt_games_cards.card and
                             tt_cards_actions.card = tt_cards.id and
                             tt_actions.id = tt_cards_actions.action and
                             tt_actions.action in ('V_','Z_','TT_') and
                             tt_times.id = tt_cards.time and
                             tt_times.time in ('".$m."','permanent','year','random')";
        $tt_gcards = mysql_query($sql_gcards);
        print mysql_error();
        while($tt_gcard = mysql_fetch_array($tt_gcards))
        {
          print "        CARD/TIME/ACTION(N):".$tt_gcard["name"]."/".$tt_gcard["time"]."/".$tt_gcard["action"]."=".(-1*$tt_gcard["param"] * gmod($tt_gcard["type"]))."\n";
          $id = $tt_gcard["action"];
          if($id=='V_'||$id=='Z_'||$id=='TT_')
          { # obrabativajem variant uhoda parametrov k sosedu
            $id = substr($id,0,strlen($id)-1); 
          }
          $gd[$id] = $gd[$id] - $tt_gcard["param"] * gmod($tt_gcard["type"]);           
        }
        # delajem pereraschjot parametrov dlja eto mesjaca
        $gt["Z"] = floor(((100 + ($g["PZ"] + $gd["PZ"])) / 100) * ($g["Z"] + $gd["Z"]) - ($g["Z"] + $gd["Z"]) * ($g["V"] + $gd["V"]) / ($g["B"] + $gd["B"]));       
        $gt["V"] = round(((100 + ($g["PV"] + $gd["PV"])) / 100) * ($g["V"] + $gd["V"]) + (($g["Z"] + $gd["Z"]) * ($g["V"] + $gd["V"])/ ($g["B"] + $gd["B"])) / ($g["E"] + $gd["E"]),0);
        # nakos travi
        $nakos = 0;
        if($m==4||$m==8||$m==12)
        {
          # udelnij nakos v grammah
          $nakos=($g["Z"] + $gd["Z"]) * ($g["TZ"] + $gd["TZ"]) / 1000;
          if($nakos>50)
          {
            $nakos=50;
          }
        }
        $gt["TT"] = ((100 + ($g["PT"] + $gd["PT"])) / 100) * ($g["TT"] + $gd["TT"]) + $nakos;

        $g["Z"] = $gt["Z"];        
        $g["V"] = $gt["V"];        
        $g["TT"] = $gt["TT"];

        if(($g["Z"] <= 0) and ($v_zajac == 1))
        {
          $g["Z"] = 1;
        }
        if(($g["V"] <= 0) and ($v_volk == 1))
        {
          $g["V"] = 1;
        }

        #paranojja
        if($m==10)
        {
          $sql_gcards = "select tt_games_cards.*
                       from tt_games_cards 
                       where tt_games_cards.sts = 0 and 
                             tt_games_cards.card = 278 and
                             tt_games_cards.on_herd  = ".$tt_herd["id"]." and
                             tt_games_cards.game = ".$tt_game["id"];
          $tt_gcards = mysql_query($sql_gcards);
          print mysql_error();
          while($tt_gcard = mysql_fetch_array($tt_gcards))
          {
            print "        PARANOJJA ON ".$tt_gcard["on_herd"]."\n";
            $g["Z"]= floor($g["PV"] * $g["B"] * $g["E"] / 100 * (-1));
            $g["V"]= round($g["PZ"] * $g["B"] / 100,0);
          }
        }

        print "        Z =".$g["Z"].", ";
        print         "PZ=".($g["PZ"]+$gd["PZ"]).", ";
        print         "V =".$g["V"].", "; 
        print         "PV=".($g["PV"]+$gd["PV"]).", ";
        print         "TT=".$g["TT"].", ";
        print         "PT=".($g["PT"]+$gd["PT"]).", ";
        print         "B =".($g["B"]+$gd["B"]).", "; 
        print         "E =".($g["E"]+$gd["E"]).", "; 
        print         "TZ=".($g["TZ"]+$gd["TZ"])."\n";

        if($g["Z"]<0)
        {
          $g["Z"]=0;
        }                        
        if($g["V"]<0)
        {
          $g["V"]=0;
        } 
        if($g["TT"]<0)
        {
          $g["TT"]=0;
        }

        # pishem istoriju
        $tt_result = mysql_query("insert into tt_games_his
                                (game,herd,year,month,Z,V,TT,PZ,PV,B,E,PT,TZ)
                                values(".$tt_game["id"].",".$tt_herd["id"].",".$next_year.",".$m.",".$g["Z"].",".$g["V"].",".$g["TT"].",
                                ".($g["PZ"]+$gd["PZ"]).",".($g["PV"]+$gd["PV"]).",".($g["B"]+$gd["B"]).",".($g["E"]+$gd["E"]).",".($g["PT"]+$gd["PT"]).",".($g["TZ"]+$gd["TZ"]).")");
        print mysql_error();
 
        #var_dump($g);
        #vse zajci umerli :(         
        if($g["Z"]==0)
        {
          break;
        }                        
      }

      # ubirajem karti (class<>change_all), kotorije igrali i vistavljajem v igru postojannije
      $sql_gcards = "select tt_games_cards.id,tt_games_cards.sts,tt_cards.name,tt_times.time
                     from tt_games_cards,tt_cards,tt_times 
                     where tt_games_cards.sts < 9 and 
                           tt_games_cards.on_herd  = ".$tt_herd["id"]." and 
                           tt_games_cards.game = ".$tt_game["id"]." and
                           tt_cards.id = tt_games_cards.card and
                           tt_cards.class <> 3  and
                           tt_times.id = tt_cards.time";
      $tt_gcards = mysql_query($sql_gcards);
      print mysql_error();
      while($tt_gcard = mysql_fetch_array($tt_gcards))
      {
        if($tt_gcard["time"]=='permanent')
        {
          if($tt_gcard["sts"]==0) 
          {
          $tt_result = mysql_query("update tt_games_cards 
                                    set tt_games_cards.sts = 1,
                                        tt_games_cards.on_year = ".$tt_game["year"]."
                                    where tt_games_cards.id = ".$tt_gcard["id"]);
          print mysql_error();
          }
          print "      LEAVE  CARD/TIME:".$tt_gcard["name"]."/".$tt_gcard["time"]."\n";
        }
        else
        {
          $tt_result = mysql_query("update tt_games_cards 
                                    set tt_games_cards.sts = 9,
                                        tt_games_cards.on_year = ".$tt_game["year"]."
                                    where tt_games_cards.id = ".$tt_gcard["id"]);
          print mysql_error();
          print "      REMOVE CARD/TIME:".$tt_gcard["name"]."/".$tt_gcard["time"]."\n";
        }
      }
      #var_dump($g); 
      
      # god proshjol, menjajem parametri stada
      $tt_result = mysql_query("update tt_games_herds set 
                                Z='".$g["Z"]."',
                                V='".$g["V"]."',
                                TT='".$g["TT"]."',
                                PZ='".$g["PZ"]."',
                                PV='".$g["PV"]."',
                                B='".$g["B"]."',
                                E='".$g["E"]."',
                                PT='".$g["PT"]."',
                                TZ='".$g["TZ"]."' where tt_games_herds.id = ".$tt_herd["id"]);
      print mysql_error();

      # stado umerlo :(
      if($g["Z"]<=0)
      {
        # pomechajem, chto stado pogiblo
        $death_year = $tt_game["year"] + 1;
        $tt_result = mysql_query("update tt_games_herds 
                                  set sts = 9,
                                      kill_year = ".$death_year."
                     where tt_games_herds.id = ".$tt_herd["id"]);
        print mysql_error();
/*
        # ubirajem s ruk karti
        $tt_result = mysql_query("update tt_games_cards 
                                  set tt_games_cards.sts = 9 
                                  where tt_games_cards.sts = 0 and 
                                        tt_games_cards.herd = ".$tt_herd["id"]);
        print mysql_error(); 
*/
        print "    HEARD IS DEAD NOW\n";
        continue; #tak kak zajtci sdohli, to dalshe ne prodolzhajem
      }
    }

    #sdes operdeljajem, skolko kart nugno razdat
    $sql_ccounts = "select tt_games_cards.id 
                   from tt_games_cards
                   where tt_games_cards.sts = 0 and
                         tt_games_cards.on_herd = 0 and
                         tt_games_cards.herd  = ".$tt_herd["id"]." and 
                         tt_games_cards.game = ".$tt_game["id"];
    $tt_ccounts = mysql_query($sql_ccounts);
    print mysql_error();
    $req_cards_count=$tt_game["ccount"];
    while($tt_ccount = mysql_fetch_array($tt_ccounts))
    {
      $req_cards_count--;
    }
    if($req_cards_count<0)
    {
      $req_cards_count=0;
    }
    print "    CARDS to ADD: ".$req_cards_count."\n";
    $req_start = 1;
#   $goroskop = ((($tt_game["year"] + 1) % 12) + 1);
    if ($listok==1)
    {
      $listok_pack = "";
      $listok_num = 1;
      $req_start = $req_start + 1;
      $sql_packs = "select tt_cards.id 
                    from tt_cards 
                    where tt_cards.rare = 2 and
                          tt_cards.type = ".$goroskop;
      $tt_packs = mysql_query($sql_packs);
      while($tt_pack = mysql_fetch_array($tt_packs))
      {
        $listok_pack[$listok_num] = $tt_pack["id"];
        $listok_num = $listok_num + 1;
      }
      $listok_num = $listok_num - 1;
      $listok_num = mt_rand(1,$listok_num);
      $tt_result = mysql_query("insert into tt_games_cards
                                (game,herd,card,year,on_herd,sts)
                                values(".$tt_game["id"].",".$tt_herd["id"].",".$listok_pack[$listok_num].",".$tt_game["year"].",0,0)");
      print mysql_error();
    }
    if ($pero==1)
    {
      $pero_pack = "";
      $pero_num = 1;
      $req_start = $req_start + 1;
      $sql_packs = "select tt_cards.id 
                    from tt_cards 
                    where tt_cards.rare = 1 and
                          tt_cards.type = ".$goroskop;
      $tt_packs = mysql_query($sql_packs);
      while($tt_pack = mysql_fetch_array($tt_packs))
      {
        $pero_pack[$pero_num] = $tt_pack["id"];
        $pero_num = $pero_num + 1;
      }
      $pero_num = $pero_num - 1;
      $pero_num = mt_rand(1,$pero_num);
      $tt_result = mysql_query("insert into tt_games_cards
                                (game,herd,card,year,on_herd,sts)
                                values(".$tt_game["id"].",".$tt_herd["id"].",".$pero_pack[$pero_num].",".$tt_game["year"].",0,0)");
      print mysql_error();
    }
    # delajem kolodu dlja razdachi
    $user_pack="";
    for($ui=$req_start;$ui<=$req_cards_count;$ui++)
    {
      #podgotavlivajem kolodu dlja ocherednogo vitaskivanija karti
      $pack="";
      $j=0;
      $sql_packs = "select tt_cards.id, 
                           tt_cards.name, 
                           tt_rares.rare,
                           tt_rares.index 
                    from tt_cards, tt_rares, tt_classes 
                    where tt_rares.id = tt_cards.rare and
                          tt_classes.id = tt_cards.class and 
                          tt_rares.rare in ('common','uncommon','rare') and
                          tt_classes.class in ('change','change_all','killprot','special2','special','want-type')";
      $tt_packs = mysql_query($sql_packs);
      while($tt_pack = mysql_fetch_array($tt_packs))
      {
        #print "  CARD:".$tt_pack["name"]."-".$tt_pack["rare"]."-".$tt_pack["index"]."\n";
        for($i=1;$i<=$tt_pack["index"];$i++)
        {
          $pack[$j++]=array("id" => $tt_pack["id"],"name" => $tt_pack["name"],"rare" => $tt_pack["rare"]);
          #print "  CARD:".$tt_pack["name"]."-".$tt_pack["rare"]."-".$tt_pack["index"]."\n";
        }    
      }
      # obrabotka kart, uvelichuvajuschih verojatnostj prihoda (class=want-type)

      $sql_gcards = "select tt_games_cards.*,tt_cards_actions.action
                       from tt_games_cards, tt_cards, tt_cards_actions 
                       where tt_games_cards.sts = 1 and 
                             tt_games_cards.on_herd = ".$tt_herd["id"]." and
                             tt_games_cards.game = ".$tt_game["id"]." and
                             tt_cards.id = tt_games_cards.card and
                             tt_cards.class = 6 and
                             tt_cards_actions.card = tt_cards.id
                       ";      
      $tt_gcards = mysql_query($sql_gcards);
      print mysql_error();
      while($tt_gcard = mysql_fetch_array($tt_gcards))
      {
        if($tt_gcard['action']==37)
        {
         $want_res = ver_res(8);
        }

        if($tt_gcard['action']==38)
        {
         $want_res = ver_res(11);
        }
        if($tt_gcard['action']==39)
        {
         $want_res = ver_res(12);
        }
        if($tt_gcard['action']==40)
        {
         $want_res = ver_res(7);
        }
        if($tt_gcard['action']==41)
        {
         $want_res = ver_res(9);
        }
        if($tt_gcard['action']==42)
        {
         $want_res = ver_res(4);
        }
        if($tt_gcard['action']==43)
        {
         $want_res = ver_res(1);
        }
        if($tt_gcard['action']==44)
        {
         $want_res = ver_res(2);
        }
        if($tt_gcard['action']==45)
        {
         $want_res = ver_res(6);
        }
        if($tt_gcard['action']==46)
        {
         $want_res = ver_res(10);
        }
        if($tt_gcard['action']==47)
        {
         $want_res = ver_res(5);
        }
        if($tt_gcard['action']==48)
        {
         $want_res = ver_res(3);
        }
      }

      # sdes nuzhno vikinut lishnije karti iz kolodi
      $sql_gcards = "select tt_games_cards.card
                     from tt_games_cards,tt_cards 
                     where tt_games_cards.sts in (0,1) and 
                           tt_games_cards.game = ".$tt_game["id"]." and
                           tt_cards.id = tt_games_cards.card and
                           tt_cards.rare = 4";
      $tt_gcards = mysql_query($sql_gcards);
      print mysql_error();
      while($tt_gcard = mysql_fetch_array($tt_gcards))
      {
        # print "    do vikidivanija redkih:".$tt_gcard["card"]."\n";
        foreach($pack as $i => $value)
        {
         if($pack[$i]["id"]==$tt_gcard["card"])
         {
           # print "    vikidivaju redkije:".$pack[$i]["id"]."\n";
           unset($pack[$i]);
         }
        }
      }
      $sql_gcards = "select tt_games_cards.card
                     from tt_games_cards,tt_cards 
                     where tt_games_cards.sts in (0,1) and 
                           tt_games_cards.game = ".$tt_game["id"]." and
                           tt_games_cards.herd = ".$tt_herd["id"]." and
                           tt_cards.id = tt_games_cards.card and
                           tt_cards.rare = 2";
      $tt_gcards = mysql_query($sql_gcards);
      print mysql_error();
      while($tt_gcard = mysql_fetch_array($tt_gcards))
      {
        # print "    do vikidivanija neobichnih:".$tt_gcard["card"]."\n";       
        foreach($pack as $i => $value)
        { 
         if($pack[$i]["id"]==$tt_gcard["card"])
         {
           # print "    vikidivaju neobichnije:".$pack[$i]["id"]."\n";
           unset($pack[$i]);
         }
        }
      }

      $user_pack[$ui]=$pack[array_rand($pack,1)];
      #var_dump($user_pack[$ui]);
      print "      CARD:".$user_pack[$ui]["id"]."-".$user_pack[$ui]["name"]."\n";
      #dobavljajem vibrannuju kartu na ruki
      $tt_result = mysql_query("insert into tt_games_cards
                                (game,herd,card,year,on_herd,sts)
                                values(".$tt_game["id"].",".$tt_herd["id"].",".$user_pack[$ui]["id"].",".$tt_game["year"].",0,0)");
      print mysql_error(); 
    }
/*
    #dobavljajem vibrannije karti na ruki
    $i=1000;
    foreach($user_pack as $ucard)
    {
      $tt_result = mysql_query("insert into tt_games_cards
                                (game,herd,card,year,on_herd,sts)
                                values(".$tt_game["id"].",".$tt_herd["id"].",".$ucard["id"].",".$tt_game["year"].",0,0)");
      print mysql_error(); 
    }
*/
/*
    #vistavljajem pervuju kartu pod snos
    #vistavljajem vtoruju kartu igrat na sebja
    $sql_snos = "select tt_games_cards.id 
                 from tt_games_cards
                 where tt_games_cards.sts = 0 and
                       tt_games_cards.on_herd = 0 and
                       tt_games_cards.herd  = ".$tt_herd["id"]." and 
                       tt_games_cards.game = ".$tt_game["id"]."
                 order by tt_games_cards.id";
    $tt_snos = mysql_query($sql_snos);
    print mysql_error();
    $i=0;
    while($tt_sno = mysql_fetch_array($tt_snos))
    {
      $i++;
      if($i==1)
      {
        $tt_result = mysql_query("update tt_games_cards set tt_games_cards.on_herd = -1 where tt_games_cards.id = ".$tt_sno["id"]);
        print mysql_error(); 
      }
      elseif($i==2)
      {
        $tt_result = mysql_query("update tt_games_cards set tt_games_cards.on_herd = ".$tt_herd["id"]." where tt_games_cards.id = ".$tt_sno["id"]);
        print mysql_error(); 
      }
    }
*/
  }
  if($tt_game["year"]>0)
  {
  # ubirajem karti (class=change_all), kotorije igrali i vistavljajem v igru postojannije
      print "    CHANGE_ALL:\n";
      $sql_gcards = "select tt_games_cards.id,tt_games_cards.sts,tt_cards.name,tt_times.time
                     from tt_games_cards,tt_cards,tt_times 
                     where tt_games_cards.sts < 9 and 
                           tt_games_cards.on_herd > 0 and 
                           tt_games_cards.game = ".$tt_game["id"]." and
                           tt_cards.id = tt_games_cards.card and
                           tt_cards.class = 3 and
                           tt_times.id = tt_cards.time";
      $tt_gcards = mysql_query($sql_gcards);
      print mysql_error();
      while($tt_gcard = mysql_fetch_array($tt_gcards))
      {
        if($tt_gcard["time"]=='permanent')
        {
          if($tt_gcard["sts"]==0) 
          {
          $tt_result = mysql_query("update tt_games_cards,tt_games 
                                    set tt_games_cards.sts = 1,
                                        tt_games_cards.on_year = ".$tt_game["year"]."
                                    where tt_games_cards.id = ".$tt_gcard["id"]." and
                                          tt_games.year <> ".$tt_game["year"]
                                   );
          print mysql_error();
          }
          print "      LEAVE  CARD/TIME:".$tt_gcard["name"]."/".$tt_gcard["time"]."\n";
        }
        else
        {
          $tt_result = mysql_query("update tt_games_cards,tt_games 
                                    set tt_games_cards.sts = 9,
                                        tt_games_cards.on_year = ".$tt_game["year"]."
                                    where tt_games_cards.id = ".$tt_gcard["id"]." and
                                          tt_games.year <> ".$tt_game["year"]
                                   );
          print mysql_error();
          print "      REMOVE CARD/TIME:".$tt_gcard["name"]."/".$tt_gcard["time"]."\n";
        }
      }
  }

  #vistavljajem pervuju kartu pod snos
  #vistavljajem vtoruju kartu igrat na sebja 
  $sql_herds = "select * from tt_games_herds where tt_games_herds.sts = 1 and tt_games_herds.game = ".$tt_game["id"];
  $tt_herds = mysql_query($sql_herds);
  while($tt_herd = mysql_fetch_array($tt_herds))
  {
    print "  DEFAULT DROP AND PLAY - HERD:".$tt_herd["id"]."\n";
    $sql_snos = "select tt_games_cards.id 
                 from tt_games_cards
                 where tt_games_cards.sts = 0 and
                       tt_games_cards.on_herd = 0 and
                       tt_games_cards.herd  = ".$tt_herd["id"]." and 
                       tt_games_cards.game = ".$tt_game["id"]."
                 order by tt_games_cards.id";
    $tt_snos = mysql_query($sql_snos);
    print mysql_error();
    $i=0;
    while($tt_sno = mysql_fetch_array($tt_snos))
    {
      $i++;
      if($i==1)
      {
        $tt_result = mysql_query("update tt_games_cards set tt_games_cards.on_herd = -1 where tt_games_cards.id = ".$tt_sno["id"]);
        print mysql_error(); 
      }
      elseif($i==2)
      {
        $tt_result = mysql_query("update tt_games_cards set tt_games_cards.on_herd = ".$tt_herd["id"]." where tt_games_cards.id = ".$tt_sno["id"]);
        print mysql_error(); 
      }
    }
  }

  # sgorajut karti umershih igrokov
  $sql_herds = "select * from tt_games_herds 
                where tt_games_herds.sts = 9 and 
                      tt_games_herds.game = ".$tt_game["id"];
  $tt_herds = mysql_query($sql_herds);
  while($tt_herd = mysql_fetch_array($tt_herds))
  {
    $kyear = $tt_game["year"] + 1;
    $tt_result = mysql_query(
                    "update tt_games_cards
                     set tt_games_cards.sts = 9, 
                         tt_games_cards.kill_year = ".$kyear."
                     where tt_games_cards.sts <> 9 and 
                           tt_games_cards.on_herd  = ".$tt_herd["id"]." and 
                           tt_games_cards.game = ".$tt_game["id"]);
    print mysql_error();
    $tt_result = mysql_query(
                    "update tt_games_cards
                     set tt_games_cards.sts = 9, 
                         tt_games_cards.kill_year = ".$kyear."
                     where tt_games_cards.sts <> 9 and 
                           tt_games_cards.herd  = ".$tt_herd["id"]." and 
                           tt_games_cards.game = ".$tt_game["id"]);
    print mysql_error();
  }
  # rastaskivanije travi umershih stad
  $kyear = $tt_game["year"] + 1;
  $sql_herds = "select * from tt_games_herds 
                where tt_games_herds.sts = 9 and
                      tt_games_herds.kill_year = ".$kyear." and
                      tt_games_herds.game = ".$tt_game["id"];
  $tt_herds = mysql_query($sql_herds);
  while($tt_herd = mysql_fetch_array($tt_herds))
  {
    $kill_num = 0;
    $kill_herd = $tt_herd["id"];
    $sql_lherds = "select tt_games_herds.id from tt_games_herds 
                    where (tt_games_herds.sts = 1 or tt_games_herds.id=".$kill_herd.")
                          and
                          tt_games_herds.game = ".$tt_game["id"]."
                          order by tt_games_herds.id";
    $tt_lherds = mysql_query($sql_lherds);
    $lh = 1;
    while($tt_lherd = mysql_fetch_array($tt_lherds))
    {
      if($tt_lherd["id"]==$kill_herd)
      {
        $kill_num = $lh;
      }
      $lherd[$lh] = $tt_lherd["id"];
      $lh = $lh + 1; 
    }
    $lh = $lh - 1;
    foreach($lherd as $key => $value)
    {
      $kill_tt = 0;
      if(sosed($key,$kill_num,$lh)==1)
      {
        $kill_tt = round($tt_herd["TT"] / 24,3);
      }
      if(sosed($key,$kill_num,$lh)==2)
      {
        $kill_tt = round($tt_herd["TT"] / 48,3);
      }
      if($kill_tt<>0)
      {
        print "   STADO ".$value." UTASCHILO TRAVU U UMERSHEGO STADA ".$kill_herd." V RAZMERE ".$kill_tt."\n";
        $tt_result = mysql_query("update tt_games_herds set TT=TT + ".$kill_tt." 
                                  where tt_games_herds.id = ".$value." and
                                        tt_games_herds.game = ".$tt_game["id"]
                                );
        print mysql_error();
      }
    }
    unset($lherd);
  }

  # konec igri
  $game_over = 1;
  $TT_end = 0;
  $kolvo_end = 0;
  $sql_herds = "select * from tt_games_herds 
                where tt_games_herds.sts = 1 and 
                      tt_games_herds.game = ".$tt_game["id"];
  $tt_herds = mysql_query($sql_herds);
  while($tt_herd = mysql_fetch_array($tt_herds))
  {
    # kaljan
    $kaljan = 1;
    $sql_gcards = "select tt_games_cards.*
                       from tt_games_cards 
                       where tt_games_cards.sts = 1 and 
                             tt_games_cards.herd > 0 and
                             tt_games_cards.card = 152 and
                             tt_games_cards.on_herd  = ".$tt_herd["id"]." and
                             tt_games_cards.game = ".$tt_game["id"];
    $tt_gcards = mysql_query($sql_gcards);
    print mysql_error();
    while($tt_gcard = mysql_fetch_array($tt_gcards))
    {
      print "        KALJAN ON ".$tt_gcard["on_herd"]."\n";
      # $kaljan = 1.1;
      if($goroskop==7)
      {
       $kaljan = 1.15;
      }
      else
      {
       $kaljan = 1.1;
      }
    }
    $game_over = 0;
    $kolvo_end = $kolvo_end  + 1;
    if ($kolvo_end==1)
    {
      $win_herd = $tt_herd["id"];
    }
    if($tt_herd["TT"] >= (1000 / $kaljan))      
    {
      $TT_end = 1;
      $tt_result = mysql_query("update tt_games_herds set sts = 2 where tt_games_herds.id = ".$tt_herd["id"]);
      print mysql_error();
    }
  }
  if(($game_over == 0) and ($TT_end == 0) and ($kolvo_end == 1))
  {
      $tt_result = mysql_query("update tt_games_herds set sts = 2 where tt_games_herds.id = ".$win_herd);
      print mysql_error();
  }
  if(($game_over == 1) or ($TT_end == 1) or ($kolvo_end == 1))
  {
    $tt_result = mysql_query("update tt_games set tt_games.sts = 0 where tt_games.id = ".$tt_game["id"]);
    print mysql_error(); 
  }
  # uvelichivajem schjotchik goda na 1
  $tt_result = mysql_query("update tt_games set tt_games.year = tt_games.year + 1 where tt_games.id = ".$tt_game["id"]);
  print mysql_error(); 
}
$tt_result = mysql_query('COMMIT;');

?>
</pre>

</body>
</html>
